// packageC/pages/package/agv_manage/lineManage/addLine/addLine.js
const mybehavior = require("../../../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/agv_dispatch_system",
    tag_id: "",
    department: "",
    storage_rack_name: "",
    storage_all_column: 1,
    data_arr: {},
    recycle_station: [],
    caozuo_show: false,
    control_val: "",
    options_destination_station: [],
    add_tag_show: true,
    storage_info: [],
    storage_info_list: [
      [
        {
          location: "",
          material_name: "",
          post_station_id: "",
          status: "",
          storage_column: 1,
          storage_row: 0x00,
          storage_station: [],
        },
        {
          location: "",
          material_name: "",
          post_station_id: "",
          status: "",
          storage_column: 1,
          storage_row: 0xff,
          storage_station: [],
        },
      ]
    ],
    options_line: [],
    post_station_list: [],
    caozuo_storage_show: false,
    control_storage_val: "",
    options_storage_station: [],
    data_index: "",

  },
  // 提交修改数据
  submit() {
    let storage_list = JSON.parse(JSON.stringify(this.data.storage_info_list))
    console.log(storage_list);
    let lists = []
    for (let i = 0; i < storage_list.length; i++) {
      let obj = {
        storage_column: i + 1,
        storage_station: storage_list[i][0].storage_station,
        storage_row_info: [
          {
            location: storage_list[i][0].location,
            material_name: storage_list[i][0].material_name,
            post_station_id: storage_list[i][0].post_station_id,
            status: storage_list[i][0].status,
            storage_row: storage_list[i][0].storage_row,
          },
          {
            location: storage_list[i][1].location,
            material_name: storage_list[i][1].material_name,
            post_station_id: storage_list[i][1].post_station_id,
            status: storage_list[i][1].status,
            storage_row: storage_list[i][1].storage_row,
          },
        ]
      }
      lists.push(obj)

    }
    let data = {
      department: this.data.department,
      new_storage_rack_name: this.data.storage_rack_name,
      storage_rack_num: this.data.data_arr.storage_rack_num,
      storage_all_column: this.data.storage_all_column,
      recycle_station: this.data.recycle_station,
      storage_info: lists,
    }
    console.log(data);
    let url = this.data.baseUrl + "/update_storage_rack_station"
    // this.post(url, data).then(res => {
    //   if (res.code == 20000) {

    //     wx.showToast({
    //       title: res.msg,
    //       icon: 'success'
    //     })

    //   } else {
    //     wx.showToast({
    //       title: res.msg,
    //       icon: 'none'
    //     })
    //   }
    // }).catch(err => {
    //   console.log(err);
    //   wx.showToast({
    //     title: err.msg,
    //     icon: 'none'
    //   })
    // })
  },
  // 货架列数变换
  columnChange(e) {
    let storage_list = JSON.parse(JSON.stringify(this.data.storage_info_list))
    this.setData({
      storage_all_column: e.detail,
    })
    let chazhi = this.data.storage_all_column - this.data.storage_info_list.length
    // 差值是正的,需要新增空白模板
    if (chazhi > 0) {
      for (let column_index = 1; column_index <= chazhi; column_index++) {
        let lists = [
          {
            location: "",
            material_name: "",
            post_station_id: "",
            status: "",
            storage_column: e.detail,
            storage_row: 0x00,
            storage_station: [],
          },
          {
            location: "",
            material_name: "",
            post_station_id: "",
            status: "",
            storage_column: e.detail,
            storage_row: 0xff,
            storage_station: [],
          },
        ]
        storage_list.push(lists)
      }
    }
    // 差值是负的,删掉多余的列数据
    else {
      storage_list.splice(this.data.storage_all_column)
    }
    this.setData({
      storage_info_list: storage_list
    })
    console.log("xxx", this.data.storage_info_list);
  },
  // 下拉选择
  getPickerVlaue1(e) {
    let key = e.target.id
    console.log(e);
    this.setData({
      [key]: e.detail

    })
    this.getLineData()
    this.getPostData()
  },
  // 空间回收站点-弹窗
  onClick() {
    let lists = JSON.parse(JSON.stringify(this.data.recycle_station))
    this.setData({
      caozuo_show: true,
      options_destination_station: lists

    })
  },
  // 空间回收站点-弹窗添加按钮是否可选
  chechAddTagBtn() {
    if (this.data.control_val != "") {
      this.setData({
        add_tag_show: false
      })
    } else {
      this.setData({
        add_tag_show: true
      })
    }
  },
  // 添加-空间回收站点-值
  addTag() {
    let lists = JSON.parse(JSON.stringify(this.data.options_destination_station))
    if (lists.includes(Number(this.data.control_val))) {
      wx.showToast({
        title: '禁止重复添加！',
        icon: 'none'
      })
      this.setData({
        control_val: "",
      })
    } else {
      lists.push(Number(this.data.control_val))
      this.setData({
        options_destination_station: lists,
        control_val: "",
        add_tag_show: true

      })
    }


  },
  // 删除-空间回收站点-值值
  deleteTag(e) {
    console.log(e.target.dataset.index);
    let lists = JSON.parse(JSON.stringify(this.data.options_destination_station))
    lists.splice(e.target.dataset.index, 1)
    this.setData({
      options_destination_station: lists,
    })
  },
  // 空间回收站点-值确认
  onConfirm() {
    let lists = JSON.parse(JSON.stringify(this.data.options_destination_station))
    this.setData({
      recycle_station: lists,
      options_destination_station: []
    })
  },
  // 取消-空间回收站点-值
  onCancel() {
    this.setData({
      caozuo_show: false,
      options_destination_station: [],
      control_val: "",
      add_tag_show: true
    })
  },



  // 接料站点-弹窗
  onStorageClick(e) {
    // i --第几列
    let i = e.target.dataset.index
    // row --该列数据
    let row = e.target.dataset.row
    // let lists = JSON.parse(JSON.stringify(this.data.storage_info_list))

    this.setData({
      data_index: i,
      caozuo_storage_show: true,
      options_storage_station: row[0].storage_station

    })
  },
  // 接料站点-弹窗添加按钮是否可选
  chechStorageAddTagBtn() {
    if (this.data.control_storage_val != "") {
      this.setData({
        add_storage_tag_show: false
      })
    } else {
      this.setData({
        add_storage_tag_show: true
      })
    }
  },
  // 添加-接料站点-值
  addStorageTag() {
    let lists = JSON.parse(JSON.stringify(this.data.options_storage_station))
    if (lists.includes(Number(this.data.control_storage_val))) {
      wx.showToast({
        title: '禁止重复添加！',
        icon: 'none'
      })
      this.setData({
        control_storage_val: "",
      })
    } else {
      lists.push(Number(this.data.control_storage_val))
      this.setData({
        options_storage_station: lists,
        control_storage_val: "",
        add_storage_tag_show: true

      })
    }


  },
  // 删除-接料站点-值值
  deleteStorageTag(e) {
    console.log(e.target.dataset.index);
    let lists = JSON.parse(JSON.stringify(this.data.options_storage_station))
    lists.splice(e.target.dataset.index, 1)
    this.setData({
      options_storage_station: lists,
    })
  },
  // 接料站点-值确认
  onStorageConfirm() {
    let lists = JSON.parse(JSON.stringify(this.data.options_storage_station))
    let storage_list = JSON.parse(JSON.stringify(this.data.storage_info_list))
    let index = this.data.data_index
    storage_list[index][0].storage_station = lists
    storage_list[index][1].storage_station = lists
    this.setData({
      storage_info_list: storage_list,
      data_index: "",
      control_storage_val: "",
      caozuo_storage_show: false,
      add_storage_tag_show: true

    })
    // console.log(this.data.storage_info_list);
  },
  // 取消-接料站点-值
  onStorageCancel() {
    this.setData({
      caozuo_storage_show: false,
      options_storage_station: [],
      control_storage_val: "",
      data_index: "",
      add_storage_tag_show: true
    })
  },

  // 查询部门
  getDepartmentData() {
    let url = this.data.baseUrl + "/get_department_list"

    let data = {
    }
    // console.log("查询部门-传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options1: res.data

        })

      } else {
        this.setData({
          options1: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options1: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询线体数据
  getLineData() {
    let url = this.data.baseUrl + "/get_line_list"

    let data = {
      department: this.data.department,
    }
    // console.log("查询线体数据-传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options_line: res.data

        })

      } else {
        this.setData({
          options_line: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options_line: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询岗位数据
  getPostData() {
    let url = this.data.baseUrl + "/get_post_station"

    let data = {
      department: this.data.department,
      current: 1,
      page_size: 9999
    }
    // console.log("查询岗位数据-传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {

        // let storage_lists = JSON.parse(JSON.stringify(this.data.storage_info))
        // for (let index = 0; index < storage_lists.length; index++) {
        //   let l = res.data.table_data.filter(item => item.line == storage_lists[index].location)
        //   storage_lists[index].material_list = l
        // }
        // let storage_info_list = []
        // for (let index = 0; index < storage_lists.length; index += 2) {
        //   storage_info_list.push(storage_lists.slice(index, index + 2))
        // }
        // console.log(storage_info_list);
        this.setData({
          post_station_list: res.data.table_data,
          // storage_info_list: storage_info_list

        })

      } else {
        this.setData({
          post_station_list: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        post_station_list: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 线体切换
  PickerChange(e) {
    let lists = JSON.parse(JSON.stringify(this.data.post_station_list))
    let storage_lists = JSON.parse(JSON.stringify(this.data.storage_info_list))
    // i --第几列
    let i = e.target.dataset.index
    // r_i --第几层
    let r_i = e.target.dataset.row_index
    // row --该列数据
    let row = e.target.dataset.row
    // id --该列数据id
    let r_id = e.target.id
    if (r_id == "location") {
      let l = lists.filter(item => item.line == e.detail)
      storage_lists[i][r_i].material_list = l
      storage_lists[i][r_i][r_id] = e.detail
      storage_lists[i][r_i].material_name = ""
      storage_lists[i][r_i].status = ''

    } else {
      storage_lists[i][r_i][r_id] = e.detail.material_name
      let l = lists.filter(item => item.line == e.detail.line && item.material_name == e.detail.material_name)
      storage_lists[i][r_i].status = "待叫料"
      if (l.length != 0) {
        storage_lists[i][r_i].post_station_id = l[0].key
      }

    }

    this.setData({
      storage_info_list: storage_lists
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDepartmentData()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})